package com.bins.code.generator.strategy.impl;

import com.bins.code.generator.config.StrategyConfig;
import com.bins.code.generator.config.builder.BaseBuilder;
import com.bins.code.generator.config.builder.ConfigBuilder;
import com.bins.code.generator.enums.TemplateEnum;
import com.bins.code.generator.function.ConverterFileName;
import com.bins.code.generator.model.AuthUtilsModel;
import com.bins.code.generator.model.po.TableInfo;
import com.bins.code.generator.strategy.IGenerator;
import org.springframework.beans.BeanUtils;

public class AuthUtilsStrategyGenerator extends AbstractStrategyGenerator implements IGenerator {

    private static final AuthUtilsStrategyGenerator generator = new AuthUtilsStrategyGenerator();

    private AuthUtilsStrategyGenerator() {
    }

    public static AuthUtilsStrategyGenerator getInstance() {
        return generator;
    }

    private AuthUtilsModel model;

    @Override
    public String getGeneratorName() {
        return TemplateEnum.AUTH_UTILS.getCode();
    }

    @Override
    public void initModel(TableInfo tableInfo) {
        ConfigBuilder configBuilder = super.getConfigBuilder();
        model = new AuthUtilsModel(configBuilder);
        BeanUtils.copyProperties(configBuilder.getStrategyConfig().authUtilsBuilder().get(), model);
        model.initCommonConfig();
        model.setTableComment("权限工具类");
        model.setClassName("AuthUtils");
        model.initCommonPackage();
    }

    public AuthUtilsModel getModel() {
        return model;
    }

    @Override
    public String getTemplateCode() {
        return model.getTemplate().getCode();
    }

    @Override
    public String getModelFileName() {
        return model.getClassName();
    }

    @Override
    public String getModelTemplatePath() {
        return getConfigBuilder().getTemplateConfig().getAuthUtils();
    }

    @Override
    public TemplateEnum getModelTemplate() {
        return model.getTemplate();
    }

    public static class Builder extends BaseBuilder {

        private final AuthUtilsStrategyGenerator generator = new AuthUtilsStrategyGenerator();

        public Builder(StrategyConfig strategyConfig) {
            super(strategyConfig);
        }

        /**
         * 格式化文件名称
         *
         * @param format 　格式
         * @return this
         */
        public AuthUtilsStrategyGenerator.Builder formatFileName(String format) {
            return convertFileName((dtoName) -> String.format(format, dtoName));
        }

        /**
         * 转换输出文件名称
         *
         * @param converter 　转换处理
         */
        public AuthUtilsStrategyGenerator.Builder convertFileName(ConverterFileName converter) {
            this.generator.model.setConverterFileName(converter);
            return this;
        }

        public AuthUtilsModel get() {
            return this.generator.model;
        }
    }
}
